package com.cskaoyan.service.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.userbean.Collect;
import com.cskaoyan.bean.userbean.CollectExample;
import com.cskaoyan.mapper.usermapper.CollectMapper;
import com.cskaoyan.vo.wx.CollectAddOrDeleteVo;
import com.cskaoyan.vo.wx.CollectList_WxVo;
import com.cskaoyan.vo.wx.CollectList_Wx_BeanVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService{

    @Autowired
    CollectMapper collectMapper;
    @Override
    public CollectAddOrDeleteVo addOrDelete(Collect collect) {
        //用来返回数据
        CollectAddOrDeleteVo collectAddOrDeleteVo = new CollectAddOrDeleteVo();

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(MyUtils.getUserId())
                .andValueIdEqualTo(collect.getValueId())
                .andTypeEqualTo(collect.getType())
                .andDeletedEqualTo(false);
        //这里返回值直接设为id
        Integer collectId = collectMapper.selectTargetId(collectExample);   //查不到数据的情况下，返回值为null

        //判断当前收藏表中 是否存在
        if(collectId != null){
            // 如果存在则删除（这里数据应该set deleted=1） 利用Id
            collect.setId(collectId);
            collect.setUpdateTime(new Date());
            collect.setDeleted(true);
            collectMapper.updateByPrimaryKeySelective(collect);
            collectAddOrDeleteVo.setType("delete");
            return collectAddOrDeleteVo;
        } else {
            //不存在则添加
            collect.setUserId(MyUtils.getUserId());
            collect.setAddTime(new Date());
            collect.setUpdateTime(new Date());
            collect.setDeleted(false);
            collectMapper.insert(collect);
            collectAddOrDeleteVo.setType("add");
            return collectAddOrDeleteVo;
        }
    }

    @Override
    public CollectList_WxVo list(int type, Integer page, Integer size) {
        CollectList_WxVo listVo = new CollectList_WxVo();   //返回对象

        PageHelper.startPage(page,size);    //分页

        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause("add_time" + " " + "desc");
        CollectExample.Criteria criteria = collectExample.createCriteria();
        collectExample.setOrderByClause("add_time desc");
        criteria.andTypeEqualTo((byte)type) //分类型
                .andUserIdEqualTo(MyUtils.getUserId())   //指定用户
                .andDeletedEqualTo(false);  //未被删除的订单

        long total = collectMapper.countByExample(collectExample);

        //这里返回的是总页数 而不是 总数
        int pageNum =(int) total/size;
        listVo.setTotalPages(pageNum);

        //用来封装CollectList_WxVo中的List<CollectList_Wx_BeanVo>
        List<CollectList_Wx_BeanVo>  collectBeanListVo = new ArrayList<>();

        //这里去获得一个符合条件的collect集合
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        for (Collect collect : collects) {
            collectBeanListVo.add(collectMapper.selectWithGoodsAndCollect(collect.getValueId(),collect.getId()));
        }
        listVo.setCollectList(collectBeanListVo);

        return listVo;
    }
}
